package com.zrc.superframe_net.room.download

import androidx.room.*

/**
 * @Author: superman
 * @CreateTime: 10/15/21
 * @Describe:
 */
@Dao
interface DownloadInfoDao {
    @Insert
    fun insert(vararg downloadInfo: DownloadInfo)

    @Update
    fun update(vararg downloadInfo: DownloadInfo): Int

    fun updateWithInsert(vararg downloadInfo: DownloadInfo){
        downloadInfo.forEach {
            val bean = find(it.url)
            if (bean != null) {
                bean.url=it.url
                bean.savePath=it.savePath
                bean.readLength=it.readLength
                bean.countLength=it.countLength
                bean.downState=it.downState
                update(bean)
            } else {
                insert(it)
            }
        }
    }

    @Query("SELECT * FROM DownloadInfo WHERE id = :id")
    fun find(id: Long): DownloadInfo?

    @Query("SELECT * FROM DownloadInfo WHERE url = :url")
    fun find(url: String): DownloadInfo?

    @Delete
    fun delete(vararg downloadInfo: DownloadInfo)

    @Query("SELECT * FROM DownloadInfo")
    fun all(): List<DownloadInfo>?
}